/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.Categories;
import util.HibernateUtil;

/**
 *
 * @author CANHNGUYEN
 */
public class CategoryDAO implements CategoryDAOInterface {

    Session session = null;

    public CategoryDAO() {
        SessionFactory sessionFactory = new HibernateUtil().getSessionFactory();
        this.session = sessionFactory.getCurrentSession();
    }

    @Override
    public List listAll() {
        List<Categories> cateList = null;
        try {
            session.beginTransaction();
            String HQL = "from Categories";
            Query query = session.createQuery(HQL);
            cateList = query.list();
            session.flush();
            session.getTransaction().commit();
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return cateList;
    }

    @Override
    /**
     * list shit
     */
    public List listById(int startId, int endId) {
        List<Categories> cateList = null;
        try {
            session.beginTransaction();
            String HQL = "from Categories where categoryId between ? and ?";
            Query query = session.createQuery(HQL);
            query.setString(0, "" + startId);
            query.setString(1, "" + endId);
            cateList = query.list();
            session.flush();
            session.getTransaction().commit();
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return cateList;

    }

    @Override
    public Categories[] findByName(String name) {
        Categories[] cateArray = null;
        try {
            session.beginTransaction();
            String HQL = "from Categories where categoryName like ?";
            Query query = session.createQuery(HQL);
            query.setString(0, "%" + name + "%");
            List cateList = query.list();
            cateArray = new Categories[cateList.size()];
            cateList.toArray(cateArray);

            session.flush();
            session.getTransaction().commit();
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return cateArray;
    }

    @Override
    public Categories getById(int categoryId) {
        Categories cate = null;
        try {
            session.beginTransaction();
            cate = (Categories) session.get(Categories.class, categoryId);
            session.getTransaction().commit();
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
        return cate;
    }

    @Override
    public void save(Categories cate) {
        try {
            session.beginTransaction();
            session.saveOrUpdate(cate);
            session.getTransaction().commit();
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
    }

    @Override
    public void update(Categories newCate, int Id) {
        try {
            session.beginTransaction();
//            session.saveOrUpdate(cate);
            session.getTransaction().commit();
        } catch (Exception ex) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            ex.printStackTrace();
        }
    }

    @Override
    public void delete(int id) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
